﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using KL.UFrame.Core.Exception;
using KL.UFrame.Data.Models;

namespace KL.UFrame.Data.Business.Impl
{
    public class AppConfigBusiness :BusinessBase, IAppConfigBusiness
    {
        public async Task<string> GetContentByKeyAsync(string key)
        {
            var result = await Db.AppConfigs.FindAsync(key);
            if(result==null) throw new ResultException(-501,"没有找到配置信息");
            return result.Content;
        }

        public async Task<bool> UpdateContentAsync(string key, string content)
        {
            var result = await Db.AppConfigs.FindAsync(key);
            if (result == null) throw new ResultException(-501, "没有找到配置信息");
            result.Content = content;
            return (await Db.SaveChangesAsync()) > 0;
        }

        public async Task<List<AppConfigModel>> GetAllAsync()
        {
            return await Db.AppConfigs.AsNoTracking()
                .Select(a=>new AppConfigModel
                {
                    Key = a.Key,
                    Label = a.Label,
                    Content = a.Content
                })
                .ToListAsync();
        }
    }
}